package jrobutt.room.level;

import jrobutt.*;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class InitialGuyMover implements Updatable, Renderable
{
    public final float duration;
    public final float linger_duration;
    
    public Guy subject;
    public Vector2 initial_position;
    public Vector2 final_position;
    public float timer;
    public float linger_timer;
    
    public TerrainGrid terrain_grid;
    
    private boolean active;
    
    public GameSprite sprite_3;
    public GameSprite sprite_2;
    public GameSprite sprite_1;
    public GameSprite sprite_linger;
    
    public float sprite_x;
    public float sprite_y;
    
    public GameSprite get_current_sprite ()
    {
        if (!active) return null;
        
        if (timer > 0.0f)
        {
            int ch = (int)Math.floor((timer / duration) * 3.0f) + 1;
            switch (ch)
            {
                case 3:
                return sprite_3;
                
                case 2:
                return sprite_2;
                
                case 1:
                return sprite_1;
                
                default:
                return sprite_3;
            }
        }
        else // assume linger_timer > 0.0f
        {
            return sprite_linger;
        }
    }
    
    /* assume initial_position != final_position */
    public InitialGuyMover (Guy subject, Vector2 final_position, TerrainGrid terrain_grid, GameSprite sprite_3, GameSprite sprite_2, GameSprite sprite_1, GameSprite sprite_linger, float sprite_x, float sprite_y)
    {
        duration = 3.0f * Settings.fps;
        linger_duration = 1.0f * Settings.fps;
        
        this.subject = subject;
        this.initial_position = new Vector2(subject.x, subject.y);
        this.final_position = final_position;
        timer = duration;
        
        subject.can_move = false;
        
        this.terrain_grid = terrain_grid;
        
        active = true;
        
        this.sprite_3 = sprite_3;
        this.sprite_2 = sprite_2;
        this.sprite_1 = sprite_1;
        this.sprite_linger = sprite_linger;
        
        this.sprite_x = sprite_x;
        this.sprite_y = sprite_y;
    }
    
    public void update ()
    {
        if (!active) return;
        
        if (timer > 0.0f)
        {
            timer -= 1.0f;
            if (timer <= 0.0f)
            {
                subject.can_move = true;
                subject.x = final_position.x;
                subject.y = final_position.y;
                
                terrain_grid.close_entrance_gate();
                
                linger_timer = linger_duration;
                return;
            }
            else
            {
                float ratio = 1.0f - (timer / duration);
                subject.x = GameMath.interpolate_linear(ratio, initial_position.x, final_position.x);
                subject.y = GameMath.interpolate_linear(ratio, initial_position.y, final_position.y);
            }
        }
        if (linger_timer > 0.0f)
        {
            linger_timer -= 1.0f;
            if (linger_timer <= 0.0f)
            {
                active = false;
                return;
            }
        }
    }
    
    public void render (Graphics2D g)
    {
        GameSprite current_sprite = get_current_sprite();
        if (current_sprite == null) return;
        
        AffineTransform prev_transform = g.getTransform();
        g.setTransform(Global.identity_transform);
        current_sprite.renderAt(g, sprite_x, sprite_y);
        g.setTransform(prev_transform);
    }
}
